'''
地城恶棍 v0.1
by Loveaabb<loveaabbcraft@163.com>
许可证: MIT
改编自 boardgamegeek 上的扑克游戏《地城恶棍》(原帖找不到了QWQ)
'''

HP_LIMIT = 20

from random import shuffle
class Card:
    def __init__(self, ctype, point):
        self.ctype = ctype
        self.point = point


class Player:
    healed = False
    def __init__(self, atk, durability, hp):
        self.hp = hp
        self.atk = atk
        self.durability = durability

    def equip(self, card):
        self.atk = card.point
        self.durability = 14

    def has_weapon(self):
        return self.atk != 0


def init_dungeon():
    dungeon = []
    for pa in range(1, 3):
        for po in range(2, 15):
            dungeon.append(Card(pa, po))
    for pa in range(3, 5):
        for po in range(2, 11):
            dungeon.append(Card(pa, po))
    shuffle(dungeon)
    return dungeon


def next_turn(dungeon, player):
    player.healed = False
    room = []
    if len(dungeon) >= 4:
        for i in range(0, 4):
            room.append(dungeon[0])
            dungeon.pop(0)
    return room


def use(room, index, player, useweapon):
    card = room[index]
    room.pop(index)
    if card.ctype in (1,2):
        if useweapon:
            if card.point > player.atk:
                player.hp -= card.point - player.atk
            player.durability = card.point
        else:
            player.hp -= card.point

    if card.ctype == 3:
        if not player.healed:
            hp = player.hp + card.point
            if hp > HP_LIMIT:
                player.hp = 20
            else:
                player.hp = hp
            player.healed = True

    if card.ctype ==4:
        player.atk = card.point
        player.durability = 14


if __name__ == "__main__":
    player = Player(0, 14, 20)
    dungeon = init_dungeon()
    pattern = ["怪物", "怪物", "药", "武器"]
    skipped = False
    break_flag = False
    useweapon = False
    game_over = False
    continue_flag = False
    lastcard = (0, 0)
    ending = ""
    while True:
        if game_over:
            break
        room = next_turn(dungeon, player)
        if not room:
            break
        while True:
            prtlst = []
            for card in room:
                ptn = pattern[card.ctype - 1]
                prtlst.append([ptn, card.point])
            print("当前房间: ")
            print(prtlst, "\n")
            print("HP:", player.hp, "牌堆剩余:", len(dungeon))
            print("武器攻击力:", player.atk, "武器耐久: ", player.durability)
            select = int(input("请选择一张牌...(输入0将该房间置底)> "))
            print("--------------------------------------")
            try:
                SelectedCard = room[select-1]
            except IndexError:
                print("请输入正确的数字！")
                continue
            if select == 0:
                if not skipped:
                    if len(room) == 4:
                        dungeon = dungeon + room
                        skipped = True
                        continue_flag = True
                        break
                    else:
                        print("你无法跳过该回合: 回合已开始")
                else:
                    print("你不能跳过该回合: 上回合已跳过")
            elif SelectedCard.ctype in (1, 2):
                if player.atk != 0 and player.durability >= room[select-1].point:
                    weapon = input("是否使用武器？(y/n): ")
                    useweapon = weapon not in ("n", "N", "NO", "No", "no", "不", "否", "不使用")
                else:
                    useweapon = False
                use(room, select - 1, player, useweapon)
            elif SelectedCard.ctype in (3, 4):
                use(room, select - 1, player, False)
            lastcard = (SelectedCard.ctype, SelectedCard.point)
            if player.hp <= 0:
                print("您已死亡！")
                game_over = True
                continue_flag = True
                ending = "death"
                break
            if len(room) == 1:
                break

        if continue_flag:
            continue
        skipped = False
    score = 0
    if ending == "death":
        sum = 0
        for card in dungeon:
            if card.ctype in (1, 2):
                sum += card.point
        score = -sum
    else:
        print("恭喜通关！")
        score = player.hp
        if lastcard[0] == 3:
            score += lastcard[1]
    print("最终分数: ", score)
